import 'dart:developer';
import 'dart:ui';

import 'package:auto_route/auto_route.dart';
import 'package:flutter/material.dart';
import 'package:lyps_music/components/info_control.dart';
import 'package:lyps_music/components/music_text_card.dart';
import 'package:lyps_music/components/top_bar_info.dart';
import 'package:lyps_music/request/api.dart';
import 'package:lyps_music/request/index.dart';
import 'package:lyps_music/store/index.dart';
import 'package:provider/provider.dart';

/// 音乐详情页
@RoutePage()
class MusicInfoPage extends StatefulWidget {
  const MusicInfoPage({super.key});

  @override
  State<StatefulWidget> createState() => _MusicInfoPageState();
}

class _MusicInfoPageState extends State<MusicInfoPage> {
  dynamic musicInfo = null;

  Widget _showMediaImage(String? path, {double? width, double? height}) {
    if (path == null) return Image.asset("assets/images/cover.jpg", width: width, height: height, fit: BoxFit.cover);
    return Image.network(MyRequest.dio.options.baseUrl + path, width: width, height: height, fit: BoxFit.cover);
  }

  @override
  void initState() {
    super.initState();
    Future.delayed(Duration.zero,(){
      var currentMusic = context.read<Store>().currentMusic;
      Api.getMusicInfo(currentMusic['_id']).then((res){
        setState(() {
          musicInfo = res.data['data'];
        });
      }).catchError((err){
        log(err.toString());
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    var musicData = context.select<Store, dynamic>((p0) => p0.currentMusic);
    return Scaffold(
      body: Stack(
        fit: StackFit.expand,
        children: [
          ImageFiltered(
            imageFilter: ImageFilter.blur(sigmaX: 5.0, sigmaY: 5.0),
            child: _showMediaImage(musicData['media_image']),
          ),
          Positioned.fill(
            child: Container(
              color: Colors.black.withOpacity(0.5),
              child: Column(
                children: [
                  TopBarInfo(),
                  Expanded(
                    child: Padding(
                      padding: const EdgeInsets.symmetric(horizontal: 50),
                      child: Row(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Container(
                            margin: const EdgeInsets.only(top: 50),
                            clipBehavior: Clip.antiAlias,
                            decoration: BoxDecoration(borderRadius: BorderRadius.circular(5)),
                            child: _showMediaImage(musicData['media_image'], width: 300, height: 300),
                          ),
                          if(musicInfo!=null)
                            Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                SizedBox(
                                  child: Column(
                                    children: [
                                      Text(musicInfo['name'],style: TextStyle(color: Colors.white,fontSize: 20),),
                                      SizedBox(height: 10),
                                      Text("作者:${musicInfo['id3']==null?"未知":musicInfo['id3']['ID3V1']['author']}",style: TextStyle(color: Colors.grey,fontSize: 12),),
                                      SizedBox(height: 10),
                                      Text("专辑:${musicInfo['id3']==null?"未知":musicInfo['id3']['ID3V1']['album']}",style: TextStyle(color: Colors.grey,fontSize: 12),),
                                    ],
                                  ),
                                ),
                                Expanded(child: MusicTextCard(textPath: musicInfo['lrc']),),
                              ],
                            ),
                        ],
                      ),
                    ),
                  ),
                  const SizedBox(height: 100),
                  SizedBox(
                    height: 100,
                    child: InfoControl(),
                  )
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }
}
